<?php

namespace app\logic;

class Login
{
    private $obj_model_name;
    private $str_model_name;
    private $str_id_name;

    public function __construct($str_model_name)
    {
        $this->str_model_name = $str_model_name;
        switch ($str_model_name) {
            case 'admin': // cms
                $this->obj_model_name = \app\model\AdminInfo::getInstance();
                $this->str_id_name = 'admin_id';
                break;
            case 'user': // 用户端
                $this->obj_model_name = \app\model\UserInfo::getInstance();
                $this->str_id_name = 'user_id';
                break;
            case 'courier': // 快递端
                $this->obj_model_name = \app\model\CourierInfo::getInstance();
                $this->str_id_name = 'courier_id';
                break;
            case 'driver': // 司机端
                $this->obj_model_name = \app\model\DriverInfo::getInstance();
                $this->str_id_name = 'driver_id';
                break;
            case 'stationpc': // 站点PC端
                $this->obj_model_name = \app\model\StationInfo::getInstance();
                $this->str_id_name = 'station_id';
                break;
            case 'stationapp': // 站点手机端
                $this->obj_model_name = \app\model\StationMemberInfo::getInstance();
                $this->str_id_name = 'station_member_id';
                break;
            default:
                return false;
        }
    }

    /**
     * 手机号码注册 或 手机验证码登录
     * @param string $str_mobile
     * @param string $str_identifying_code
     * @param string $str_pass_key
     * @param string $str_platform
     * @param string $str_password
     */
    public function registerOrMobileIdentifyingCodeLogin($str_mobile, $str_identifying_code, $str_pass_key, $str_platform, $str_password = '')
    {
        _toValidate('mobile', $str_mobile, 11002); // 手机号码格式不正确

        _toValidate('verify_code', $str_identifying_code, 11004); // 验证码格式不正确

        if ($str_password != '') {
            _toValidate('login_pwd', $str_password, 12017); // 登录密码格式不正确
        }

        // 验证短信验证码是否正确
        $bool_result = \app\logic\sms\SMS::getInstance()->verifySMS('login', $str_mobile, $str_pass_key, $str_identifying_code, $this->str_model_name);
        _toCompare($bool_result, '===', false, 11005); // 验证码不正确

        $int_id = $this->obj_model_name->checkIsSetAccount($str_mobile, $this->str_id_name);

        if (!($int_id > 0)) {
            // 添加用户信息-注册
            $int_id = $this->obj_model_name->fastRegister($str_mobile);
        }

        if ($str_password != '') {
            // 更新登录密码
            $this->obj_model_name->updateLoginPassword($int_id, $this->str_id_name, $str_password);
        }

        $obj = new Passport($this->str_model_name);

        $str_passport = $obj->getPassport($int_id, $str_platform, $this->str_id_name, true); // 获取passport

        _returnData(['passport' => $str_passport]);
    }

    /**
     * 账号密码登录(后台专用)
     * @param $str_account_number
     * @param $str_password
     * @param $str_platform
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function accountPasswordLogin($str_account_number, $str_password, $str_platform)
    {
        _toValidate('account', $str_account_number, 12016); // 登录账号不正确

        _toValidate('login_pwd', $str_password, 12017); // 登录密码格式不正确

        $int_id = $this->obj_model_name->checkIsSetAccount($str_account_number, $this->str_id_name, 'login_account');
        _toCompare(($int_id > 0), '===', false, 12088); // 登录账号不存在

        $bool_result = $this->obj_model_name->checkLoginPasswordIsRight($int_id, $this->str_id_name, $str_password);
        _toCompare($bool_result, '!=', true, 12017); // 登录密码不正确`

        $obj = new Passport($this->str_model_name);

        $str_passport = $obj->getPassport($int_id, $str_platform, $this->str_id_name, true); // 获取passport

        _returnData(['passport' => $str_passport]);
    }

    /**
     * 手机号码和密码登录
     * @param $str_mobile
     * @param $str_password
     * @param $str_platform
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function mobilePasswordLogin($str_mobile, $str_password, $str_platform)
    {
        _toValidate('mobile', $str_mobile, 11002); // 手机号码格式不正确

        _toValidate('login_pwd', $str_password, 12017); // 登录密码格式不正确

        $int_id = $this->obj_model_name->checkIsSetAccount($str_mobile, $this->str_id_name);
        _toCompare(($int_id > 0), '===', false, 19011); // 手机号码未注册

        $bool_result = $this->obj_model_name->checkLoginPasswordIsRight($int_id, $this->str_id_name, $str_password);
        _toCompare($bool_result, '!=', true, 12017); // 登录密码不正确`

        $obj = new Passport($this->str_model_name);

        $str_passport = $obj->getPassport($int_id, $str_platform, $this->str_id_name, true); // 获取passport

        _returnData(['passport' => $str_passport]);
    }

    /**
     * 忘记密码-验证码手机号码
     * @param $str_mobile
     * @param $str_identifying_code
     * @param $str_pass_key
     */
    public function forgetPasswordToCheckMobile($str_mobile, $str_identifying_code, $str_pass_key)
    {
        _toValidate('mobile', $str_mobile, 11002);

        _toValidate('verify_code', $str_identifying_code, 11004);

        $bool_result = \app\logic\sms\SMS::getInstance()->verifySMS('forget', $str_mobile, $str_pass_key, $str_identifying_code, $this->str_model_name);
        _toCompare($bool_result, '===', false, 11005); // 验证码不正确

        $int_id = $this->obj_model_name->checkIsSetAccount($str_mobile, $this->str_id_name);
        _toCompare(($int_id > 0), '===', false, 19011); // 手机号码未注册

        // 设置随机密钥用于找回密码接口使用
        $str_key = $this->saveForgetCache($str_mobile, $int_id);

        _returnData(['key' => $str_key]);
    }

    /**
     * 忘记密码-重置登录密码
     * @param $str_password
     * @param $str_key
     * @param $str_platform
     */
    public function forgetPasswordToResetPassword($str_password, $str_key, $str_platform)
    {
        _toValidate('login_pwd', $str_password, 19013);

        _toValidate('passport', $str_key, 19029); // 密钥不正确

        $int_id = $this->getForgetCache($str_key);
        _toCompare(($int_id > 0), '===', false, 19029); // 密钥不正确

        $this->obj_model_name->setLoginPassword($int_id, $this->str_id_name, $str_password);

        $obj = new Passport($this->str_model_name);

        $str_passport = $obj->getPassport($int_id, $str_platform, $this->str_id_name, true); // 获取passport

        _returnData(['passport' => $str_passport]);
    }

    /**
     * 保存用户编号和获取密钥
     * @param $str_mobile
     * @param $int_user_id
     * @return string
     */
    private function saveForgetCache($str_mobile, $int_id)
    {
        $str_key = md5($str_mobile . '_' . date('Y-m-d H:i:s') . '_' . rand(100000, 999999));
        \think\Cache::set($str_key . '_' . $this->str_model_name, $int_id);
        return $str_key;
    }

    /**
     * 根据密钥获取用户编号
     * @param $str_key
     * @return int
     */
    private function getForgetCache($str_key)
    {
        return (int)\think\Cache::get($str_key . '_' . $this->str_model_name);
    }
}
